#include "Content.h"

namespace cm_nat
{
    Content::Content()
    {

    }

    Content::~Content()
    {
        cm_xmpp::util::clearList( m_transports );
    }


    void Content::setCreator(const std::string &creator)
    {
        m_creator = creator;
    }

    std::string Content::getCreator()
    {
        return m_creator;
    }

    void Content::setName(const std::string &name )
    {
        m_name = name;
    }

    std::string Content::getName()
    {
        return m_name;
    }

    void Content::setSenders(const std::string &senders)
    {
        m_senders = senders;
    }
     
    std::string Content::getSenders()
    {
        return m_senders;
    }


    bool Content::addTransport(Transport* p_transport)
    {
        if( !p_transport )
            return false;

        m_transports.push_back(p_transport);

        return true;
    }

    bool Content::parse(Tag* p_tag)
    {
        if( p_tag->name()!="content" )
            return false;

        m_creator = p_tag->findAttribute("creator");
        m_senders = p_tag->findAttribute("senders");
        m_name = p_tag->findAttribute("name");

        const TagList& transports = p_tag->findChildren( "transport" );
        TagList::const_iterator it = transports.begin();
        for( ; it != transports.end(); ++it ){
            Transport* p_trans = new Transport;
            p_trans->parse( (*it) );
            addTransport(p_trans);
        }

        return true;
    }

    Tag* Content::tag() const
    {
        Tag* p_tag = new Tag("content");
        if( !m_creator.empty() ){
            p_tag->addAttribute( "creator", m_creator );
        }

        if( !m_name.empty()){
            p_tag->addAttribute("name", m_name );
        }

        if( !m_senders.empty() ){
            p_tag->addAttribute("senders", m_senders );
        }
        
        TransportList::const_iterator it= m_transports.begin();
        for(; it!=m_transports.end(); ++it ){
            p_tag->addChild( (*it)->tag() );
        }
        return p_tag;
    }
}